<?xml version="1.0" encoding="windows-1250"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" encoding="windows-1250" indent="yes"/> 

  <xsl:template match="@*">
    <xsl:copy/> 
  </xsl:template>

  <xsl:template match="/">
    <CViews>
      <xsl:apply-templates select="/CViews/CView"/>
    </CViews>
  </xsl:template>
 
  <xsl:template match="CView">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:copy-of select="CVTitles"/>
      <xsl:apply-templates select="CVInclude"/>
    </xsl:copy>
  </xsl:template>

 <xsl:template match="CVInclude">
   <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:copy-of select="Concept"/>    
     <Keys>
       <xsl:for-each select="CVIProperties/CVIProperty[@key='1' and PropGroup/@basegroup='true']">
         <xsl:sort select="@position" data-type="number" order="ascending"/>
         <xsl:variable name="propertylow">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="@name"/></xsl:with-param></xsl:call-template>
         </xsl:variable>
         <Binding property="{@name}" propertylow="{$propertylow}" inattr="{@attr}"/>
       </xsl:for-each>
     </Keys>
     <xsl:copy-of select="CVITitles"/>
     <xsl:apply-templates select="CVIProperties"/>
     
     <xsl:call-template name="ExtConcepts"/>
     <xsl:apply-templates select="CVIRels"/>
     <xsl:copy-of select="CVIStyles"/>
     <xsl:copy-of select="CVICGroups"/>
     <xsl:copy-of select="ConsistencyChecks"/>
   </xsl:copy>
 </xsl:template>


 <xsl:template name="ExtConcepts">
     <ExtConcepts>
       <xsl:for-each select="CVIProperties/CVIProperty[@extconcept and @extconcept='yes']">
         <xsl:if test="not(preceding-sibling::CVIProperty[@path=current()/@path])">
           <xsl:variable name="allprops" select="../../CVIProperties/CVIProperty[@path=current()/@path]"/>
           <xsl:variable name="bindings" select="$allprops[@defaulttype='ref' or @defaulttype='refi']"/>
           <xsl:variable name="oncreate" select="number(count($allprops[@oncreate='show'])>0)"/>
           <xsl:variable name="onedit" select="number(count($allprops[@onedit='show'])>0)"/>
           <ExtConcept concept="{@concept}" path="{@path}" insert="{$oncreate}" update="{$onedit}">
             <xsl:choose>
               <xsl:when test="count($bindings)>0">
                 <xsl:attribute name="defaulttype"><xsl:value-of select="$bindings[1]/@defaulttype"/></xsl:attribute>
                 <xsl:for-each select="$bindings">
                   <Binding p1="{@simplename}" p2="{@default}"/>
                 </xsl:for-each>
               </xsl:when>
             </xsl:choose>
           </ExtConcept>
         </xsl:if>
       </xsl:for-each>
     </ExtConcepts>
 </xsl:template>

 <xsl:template match="CVIRels">
   <xsl:copy>
     <xsl:apply-templates select="CVIRel"/>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="CVIRel">
   <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:copy-of select="CVIRBindings"/>
     <xsl:apply-templates select="CVInclude"/>
   </xsl:copy>
 </xsl:template>


 <xsl:template match="CVIProperties">
   <xsl:copy>
     <xsl:apply-templates select="CVIProperty">
       <xsl:sort select="@position" data-type="number" order="ascending"/>
     </xsl:apply-templates>
     <CVISeparator/>
     <xsl:call-template name="getOneToOneProps">
       <xsl:with-param name="cvi" select=".."/>
     </xsl:call-template>
   </xsl:copy>
 </xsl:template>


 <xsl:template match="CVIProperty">
   <xsl:choose>
     <xsl:when test="self::node()[not(contains(@name,'/'))]">
       <!-- internal properties -->
       <xsl:copy>
         <xsl:apply-templates select="@*"/>
         <xsl:attribute name="formcode">
           <xsl:call-template name="toLowerCase"><xsl:with-param name="prm"><xsl:value-of select="translate(concat(../../@nickname,@name),'_','x')"/></xsl:with-param></xsl:call-template>
         </xsl:attribute>
         <xsl:copy-of select="DefaultFilter"/>
         <xsl:copy-of select="PropGroup"/>       
         <xsl:copy-of select="RefProp"/>       
         <xsl:copy-of select="CVIPTitles"/>       
         <xsl:copy-of select="CPStaticValues"/>       
       </xsl:copy>

     </xsl:when>
     <xsl:otherwise>
       <!-- external properties -->
       <xsl:copy>

         <!-- look up the property, copy the facts here and correct them -->
         <xsl:variable name="cvipath"><xsl:value-of select="../../@rootnickpath"/>/<xsl:value-of select="@path"/></xsl:variable>
         <xsl:variable name="cvi" select="../../CVIRels/CVIRel/.//CVInclude[@rootnickpath=$cvipath]"/>
         
         <xsl:choose>
           <xsl:when test="$cvi">
             <xsl:variable name="cvip" select="$cvi/CVIProperties/CVIProperty[@name=current()/@simplename]"/>
             <xsl:apply-templates select="$cvip/@*"/>
             <xsl:apply-templates select="@*"/>
             <xsl:copy-of select="DefaultFilter"/>
             <xsl:copy-of select="$cvip/RefProp"/>       
             <xsl:call-template name="CVIPTitles">
               <xsl:with-param name="cvip1" select="."/>
               <xsl:with-param name="cvip2" select="$cvip"/>
             </xsl:call-template>
           </xsl:when>
           <xsl:otherwise>
             <xsl:variable name="cvi2" select="../../CVIRels/CVIRel/.//CVInclude[@rootpath=$cvipath]"/>
             <xsl:choose>
               <xsl:when test="$cvi2">
                 <xsl:variable name="cvip" select="$cvi2/CVIProperties/CVIProperty[@name=current()/@simplename]"/>
                 <xsl:apply-templates select="$cvip/@*"/>
                 <xsl:apply-templates select="@*"/>
                 <xsl:copy-of select="DefaultFilter"/>
                 <xsl:copy-of select="$cvip/RefProp"/>       
                 <xsl:call-template name="CVIPTitles">
                   <xsl:with-param name="cvip1" select="."/>
                   <xsl:with-param name="cvip2" select="$cvip"/>
                 </xsl:call-template>
               </xsl:when>
               <xsl:otherwise>
                 <xsl:apply-templates select="@*"/>
                 <xsl:copy-of select="DefaultFilter"/>
                 <xsl:copy-of select="RefProp"/>       
                 <xsl:copy-of select="CVIPTitles"/>       
               </xsl:otherwise>
             </xsl:choose>
           </xsl:otherwise>
         </xsl:choose>
         
       </xsl:copy>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>


 <xsl:template name="CVIPTitles">
   <xsl:param name="cvip1"/>
   <xsl:param name="cvip2"/>
   <CVIPTitles>
     <xsl:for-each select="$cvip1/CVIPTitles/CVIPTitle">
       <xsl:copy-of select="."/>
     </xsl:for-each>
     <xsl:for-each select="$cvip2/CVIPTitles/CVIPTitle">
       <xsl:variable name="t" select="$cvip1/CVIPTitles/CVIPTitle[@id_lang=current()/@id_lang]"/>
       <xsl:choose>
         <xsl:when test="not($t)">
           <xsl:copy-of select="."/>
         </xsl:when>
       </xsl:choose>
     </xsl:for-each>
   </CVIPTitles>
 </xsl:template>

 
  <xsl:template name="getOneToOneProps">
    <xsl:param name="cvi"/>
    <xsl:param name="nickpath"></xsl:param>
    
    <xsl:for-each select="$cvi/CVIRels/CVIRel[@cardinality='1:1']/CVInclude">
      <xsl:variable name="nickpathnew">
        <xsl:choose>
          <xsl:when test="$nickpath and $nickpath!=''">
            <xsl:value-of select="$nickpath"/>/<xsl:value-of select="@nickname"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@nickname"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:for-each select="CVIProperties/CVIProperty">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:attribute name="name"><xsl:value-of select="$nickpathnew"/>/<xsl:value-of select="@name"/></xsl:attribute>          
          <PropGroup>
            <xsl:apply-templates select="PropGroup/@*"/>
            <xsl:attribute name="basegroup">false</xsl:attribute>
            <xsl:attribute name="reltype">1:1</xsl:attribute>
          </PropGroup>            
          <xsl:copy-of select="CVIPTitles"/>
          <xsl:copy-of select="RefProp"/>
        </xsl:copy>
      </xsl:for-each>
      <xsl:call-template name="getOneToOneProps">
        <xsl:with-param name="cvi" select="."/>
        <xsl:with-param name="nickpath"><xsl:value-of select="$nickpathnew"/></xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>  


  <xsl:template name="toLowerCase">
    <xsl:param name="prm"/>
    <xsl:value-of select="translate($prm,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
  </xsl:template>  





</xsl:stylesheet>
       
 
